from PyQt4 import QtGui, QtCore
import event_viewer_base
import lucid.core
from pydispatch import dispatcher
import lucid.plugins.fc_editor.fc_editors

class NodeCoreEditorViewPlugin(lucid.core.IViewPlugin):
   """ Viewer that displays a node editor panel.  
   
       Acts as a view that holds an editor for the currently selected node core.
       When a node is selcted, find the core and see if we have a plugin.
       If we do, then add that plugin widget and set it up to edit the core.
       
       @var mNCEditor: The active nc editor plugin.  None if none active.
       @var mNCEditorPlugins: List of available core plugins.
       @var widget: The widget that we are holding.  This will be used as the "view".
   """
   def __init__(self):
      lucid.core.IViewPlugin.__init__(self)
      self.widget = NodeCoreEditorWidget(self)      
      
      # Connect signals
      dispatcher.connect(self.onNodeSelected, lucid.core.signal.SelectedNodeChanged)
      
      # Trigger select off the bat
      self.onNodeSelected()
      
   @staticmethod
   def getName():
      return "Node Core Editor"
      
   def getViewWidget(self):
      return self.widget

   def onNodeSelected(self):
      """ Try to find editor for the node. """
      env = lucid.core.Environment()
      cur_node = env.selectedNode
      new_plugin = None
   
      if cur_node:
         cur_core = cur_node.getCore()
         if cur_core:
            core_type = type(cur_core)
            new_plugin = lucid.plugins.fc_editor.fc_editors.buildFcEditorPlugin(core_type)
            new_plugin.setFieldContainer(cur_core)            
      self.setFcEditPlugin(new_plugin)      
         

   def setFcEditPlugin(self, plugin):
      if None == plugin:  
         self.widget.setEditorWidget(None)
      else:
         self.widget.setEditorWidget(plugin.getEditorWidget())
      
   def getIcon(self):
      return QtGui.QIcon(":/icons/data/icons/node_editor_16.png")


class NodeCoreEditorWidget(QtGui.QWidget):
   """ Simple container widget. """
   def __init__(self, plugin, parent=None):      
      QtGui.QWidget.__init__(self, parent)
      self.mNCEditViewPlugin = plugin      
      self.setupUi(self)      
      self.mCurEditorWidget = None  # The current editor widget being used

   def setupUi(self, Form):
      Form.setObjectName("NodeEditorWidget")
      
      self.vboxlayout = QtGui.QVBoxLayout(Form)
      self.vboxlayout.setMargin(0)
      self.vboxlayout.setSpacing(0)
      self.vboxlayout.setObjectName("vboxlayout")
      
      self.mEmptyLabel = QtGui.QLabel(Form)
      self.mEmptyLabel.setObjectName("empty_label")
      self.mEmptyLabel.setText("Select a Node Core")
      self.mEmptyLabel.setAlignment(QtCore.Qt.AlignCenter)
      self.vboxlayout.addWidget(self.mEmptyLabel)


   def setEditorWidget(self, widget):
      # If we are setting to null and we already have empty widget
      if not widget and not self.mCurEditorWidget:
         return
            
      # remove current
      if self.mCurEditorWidget:
         self.vboxlayout.removeWidget(self.mCurEditorWidget)
         self.mCurEditorWidget.setParent(None)
      else:
         self.vboxlayout.removeWidget(self.mEmptyLabel)
         self.mEmptyLabel.setParent(None)
      
      # Add new
      if widget:
         self.vboxlayout.addWidget(widget)         
      else:
         self.vboxlayout.addWidget(self.mEmptyLabel)
      self.mCurEditorWidget = widget
      